package gamesys.games.particlesystem.graphics
{
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * @author robt
	 */
	public class BitmapAnimationWrapper extends Sprite
	{
		private var _bitmapAnimation : BitmapAnimation;
		private var _bitmapCanvas : BitmapCanvas;

		public function BitmapAnimationWrapper(bitmapAnimation : BitmapAnimation)
		{
			_bitmapAnimation = bitmapAnimation;

			_bitmapCanvas = new BitmapCanvas(bitmapAnimation.width, bitmapAnimation.height);
			addChild(_bitmapCanvas);

			_bitmapCanvas.x = bitmapAnimation.x;
			_bitmapCanvas.y = bitmapAnimation.y;

			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}

		private function onAddedToStage(event : Event) : void
		{
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onRemovedFromStage(event : Event) : void
		{
			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(event : Event) : void
		{
			_bitmapCanvas.clear();
			_bitmapCanvas.copyPixels(_bitmapAnimation.getNextFrame(), 0, 0);
		}
	}
}
